/*
 * Copyright (c) 2021 SECURITY
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#if defined(CONFIG_LMS_SHELL)
#include <random/rand32.h>
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <shell/shell.h>
#include "mbedtls/lms.h"
#include "lmots.h"

typedef struct data_tag {
	uint8_t *x;
	uint32_t len;
} data_t;

int mbedtls_test_rnd_std_rand(void *rng_state,
								unsigned char *output,
								size_t len)
{
#if !defined(__OpenBSD__) && !defined(__NetBSD__)
	size_t i;

	if (rng_state != NULL) {
		rng_state  = NULL;
	}

	for (i = 0; i < len; ++i) {
		sys_rand_get(&output[i], 1);
	}
#else
	if (rng_state != NULL) {
		rng_state = NULL;
	}

	arc4random_buf(output, len);
#endif /* !OpenBSD && !NetBSD */

	return 0;
}

void dump_key(char *text, unsigned char *key, size_t key_len)
{
	int i;

	printf("%s\n", text);
	for (i = 0; i < key_len; i++)
	{
		printf("0x%02x, ", key[i]);

		if (i%16 == 15)
			printf("\n");
	}
	printf("\n");
}

#if defined(MBEDTLS_LMS_PRIVATE)
/*
 * LM-OTS test case: to test 
 * 1. generate key pair
 * 2. sign
 * 3. verify 
 */
void lmots_sign_verify_test1(const struct shell *shell, data_t *msg, data_t *key_id, int leaf_id,
						data_t *seed)
{
	mbedtls_lmots_public_t pub_ctx;
	mbedtls_lmots_private_t priv_ctx;
	unsigned char sig[MBEDTLS_LMOTS_SIG_LEN(MBEDTLS_LMOTS_SHA256_N32_W8)];
	unsigned char *key;
	int key_size = 128;
	size_t key_len;

	mbedtls_lmots_public_init(&pub_ctx);
	mbedtls_lmots_private_init(&priv_ctx);
	if(mbedtls_lmots_generate_private_key(&priv_ctx, MBEDTLS_LMOTS_SHA256_N32_W8,key_id->x, leaf_id, seed->x, seed->len) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

	if(mbedtls_lmots_calculate_public_key(&pub_ctx, &priv_ctx) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

	key = calloc(1, key_size);
	if (key == NULL)
	{
		printf("can't allocate memory\n");
		goto exit;
	}
	mbedtls_lmots_export_public_key(&pub_ctx, key, key_size, &key_len);

//	printf("key_len = %d\n", key_len);
//	dump_key("public key :", key, key_len);
	free(key);

	if(mbedtls_lmots_sign(&priv_ctx, &mbedtls_test_rnd_std_rand, NULL, msg->x, msg->len, sig, sizeof(sig), NULL) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

//	dump_key("sig :", sig, sizeof(sig));
	if(mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig))!= 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

	printf("%s :: verify ok\n", __FUNCTION__);
exit:
	mbedtls_lmots_public_free(&pub_ctx);
	mbedtls_lmots_private_free(&priv_ctx);
}

/*
 * LMS test case: to test 
 * 1. generate key pair
 * 2. sign
 * 3. verify 
 */
void lms_sign_verify_test(const struct shell *shell, data_t *msg, data_t *seed)
{
	int ret;
	mbedtls_lms_public_t pub_ctx;
	mbedtls_lms_private_t priv_ctx;
	unsigned char sig[MBEDTLS_LMS_SIG_LEN(MBEDTLS_LMS_SHA256_M24_H5, MBEDTLS_LMOTS_SHA256_N32_W8)];
//	uint8_t pub_key[64];
//	size_t pub_key_len;

	mbedtls_lms_public_init(&pub_ctx);
	mbedtls_lms_private_init(&priv_ctx);

	ret = mbedtls_lms_generate_private_key(&priv_ctx, MBEDTLS_LMS_SHA256_M24_H5,
										MBEDTLS_LMOTS_SHA256_N32_W8,
										mbedtls_test_rnd_std_rand, NULL,
										seed->x, seed->len);
	if (ret != 0) {
		printf("%s :: %d : failed, ret = %x\n", __FUNCTION__, __LINE__, -ret);
		goto exit;
	}

	if (mbedtls_lms_calculate_public_key(&pub_ctx, &priv_ctx) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

//	pub_key_len = 0;
//	mbedtls_lms_export_public_key(&pub_ctx, pub_key, sizeof(pub_key), &pub_key_len);
//	printf("pub_key_len = %d\n", pub_key_len);
//	dump_key("pub_key", pub_key, pub_key_len);

	if (mbedtls_lms_sign(&priv_ctx, mbedtls_test_rnd_std_rand, NULL, msg->x, msg->len, sig, sizeof(sig), NULL) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

//	dump_key("sig :", sig, sizeof(sig));
	if (mbedtls_lms_verify(&pub_ctx, msg->x, msg->len, sig, sizeof(sig)) != 0) {
		printf("%s :: %d : failed\n", __FUNCTION__, __LINE__);
		goto exit;
	}

	printf("%s :: verify ok\n", __FUNCTION__);
exit:
	mbedtls_lms_public_free(&pub_ctx);
	mbedtls_lms_private_free(&priv_ctx);
}
#endif

/*
 * LM-OTS test case: to verify digest with public key and message data
 */
int lmots_sign_verify_test2(const struct shell *shell, data_t *msg)
{
	uint8_t test_pubkey[] = {0x00, 0x00, 0x00, 0x04, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
					0x30, 0x30, 0x30, 0x30, 0x00, 0x00, 0x00, 0x0c, 0x06, 0x48, 0x01, 0x85, 0x67, 0x8d, 0xf2, 0x7f,
					0xad, 0xba, 0xa2, 0x6e, 0x9c, 0xcc, 0xf6, 0x9c, 0xa8, 0xcd, 0x17, 0x03, 0x9a, 0x7a, 0x91, 0x3a,
					0x5a, 0x2f, 0x29, 0x63, 0x61, 0x5a, 0xf6, 0x1b};
	uint8_t test_dgst[] = {0x00, 0x00, 0x00, 0x04, 0x15, 0x18, 0x5a, 0x58, 0xc2, 0x3f, 0xca, 0xfb, 0xdf, 0x46, 0xff, 0xc1,
					0x9c, 0xfc, 0x8e, 0x28, 0xe8, 0x63, 0xc4, 0xda, 0x7b, 0x8a, 0x71, 0x8e, 0x46, 0x14, 0xbd, 0xee,
					0xb0, 0x41, 0x71, 0xc3, 0x37, 0xdd, 0xc6, 0x7d, 0x0b, 0xcb, 0xfb, 0xbe, 0xc4, 0x17, 0x41, 0xd2,
					0xac, 0x6a, 0x45, 0x7f, 0x8e, 0xd9, 0x47, 0x73, 0xaf, 0xcd, 0x91, 0x7c, 0x14, 0x1c, 0x50, 0xa7,
					0x64, 0x7e, 0xcd, 0xa5, 0x80, 0xb8, 0x66, 0xcb, 0x38, 0xea, 0x8c, 0xb5, 0x61, 0x57, 0x21, 0xdf,
					0x31, 0x5a, 0x83, 0xba, 0x0a, 0xea, 0xac, 0x2e, 0x90, 0x31, 0xc7, 0x0e, 0xf9, 0x5e, 0x17, 0xca,
					0x65, 0xf8, 0x56, 0x2b, 0x32, 0x32, 0x22, 0x9f, 0x57, 0x55, 0xd3, 0x23, 0x1c, 0x5f, 0x2d, 0xa9,
					0x5b, 0x84, 0x73, 0xa7, 0x80, 0x17, 0x6d, 0x05, 0x22, 0xda, 0x38, 0x1c, 0xed, 0xcf, 0xee, 0xb8,
					0xec, 0xa7, 0x6a, 0xd3, 0xe3, 0x9f, 0x1e, 0x88, 0x9f, 0x08, 0xd1, 0xb0, 0x15, 0xd5, 0x48, 0x9b,
					0xe0, 0xa1, 0xbb, 0x72, 0x40, 0x07, 0x3f, 0x3f, 0x6e, 0x80, 0xc5, 0x9e, 0xbe, 0xee, 0x23, 0x95,
					0x8b, 0x33, 0x4e, 0xa5, 0x8f, 0xe3, 0x74, 0xfc, 0x0a, 0xbd, 0x36, 0xea, 0xb8, 0xbe, 0xfa, 0xd2,
					0x59, 0x14, 0x57, 0xfa, 0xf7, 0x1d, 0xd5, 0xef, 0x82, 0xef, 0xd0, 0xf2, 0x0e, 0x94, 0xf4, 0x08,
					0xb2, 0xf3, 0x7d, 0xae, 0xbc, 0xef, 0x8c, 0x91, 0x60, 0xa7, 0x5b, 0x9c, 0x8b, 0xd4, 0x42, 0xb6,
					0x50, 0x02, 0x89, 0x6b, 0xbb, 0x4d, 0x74, 0x70, 0x9f, 0x6b, 0x84, 0xc8, 0x51, 0x51, 0x03, 0x0c,
					0x04, 0x63, 0x40, 0xfa, 0xe6, 0x3e, 0xed, 0xb4, 0x8a, 0xe3, 0xe2, 0xc4, 0x6a, 0xad, 0xcc, 0xfe,
					0x90, 0x6e, 0xe3, 0x65, 0xf9, 0xab, 0x18, 0xe2, 0x86, 0xe6, 0x01, 0x2f, 0x23, 0x76, 0xa0, 0x80,
					0x17, 0x5c, 0xf4, 0x5f, 0x8b, 0x3e, 0xe1, 0x26, 0x3c, 0x95, 0x69, 0x8b, 0xfb, 0xbe, 0xce, 0x47,
					0xac, 0x3e, 0x8e, 0xef, 0x80, 0x88, 0xde, 0x8f, 0x54, 0x4c, 0xbe, 0xfa, 0xdd, 0xbe, 0x15, 0x03,
					0x1b, 0x57, 0xbc, 0x30, 0xbf, 0xdb, 0xd0, 0x64, 0xfc, 0x0f, 0x95, 0x89, 0x65, 0xd6, 0x07, 0x47,
					0xb7, 0xc0, 0xd9, 0xb1, 0x20, 0xa8, 0xca, 0xad, 0x04, 0x83, 0x37, 0x45, 0x62, 0xfe, 0xbc, 0xd2,
					0xaa, 0x2d, 0xe1, 0x0d, 0x0a, 0xad, 0xa8, 0x71, 0xee, 0xf0, 0x6f, 0xe6, 0x22, 0x33, 0xad, 0xbe,
					0x7c, 0xb8, 0x10, 0x23, 0x19, 0xca, 0xbb, 0xa8, 0xef, 0x0d, 0x89, 0x4a, 0xf6, 0x15, 0x91, 0x33,
					0xeb, 0x64, 0x47, 0xdf, 0xb9, 0x10, 0x7b, 0xb3, 0xf1, 0x47, 0x5e, 0x45, 0x3d, 0xd2, 0x82, 0x39,
					0x0f, 0x28, 0x67, 0xc8, 0x52, 0xf2, 0x1a, 0xad, 0xf8, 0xbc, 0x6c, 0xe6, 0x98, 0x2b, 0xf8, 0x59,
					0xa1, 0xcb, 0x91, 0xaf, 0x36, 0xb0, 0x43, 0x31, 0xf7, 0xdc, 0x48, 0xdc, 0xd9, 0x67, 0x7d, 0x11,
					0x1c, 0x6d, 0x2f, 0xcc, 0x27, 0x94, 0x2e, 0x03, 0x85, 0xed, 0x42, 0x8b, 0x97, 0x2d, 0xb4, 0x48,
					0x10, 0x21, 0x51, 0xa7, 0x43, 0x3b, 0x4f, 0x88, 0x2e, 0x97, 0x7b, 0x41, 0x2e, 0x59, 0x46, 0xc2,
					0xb5, 0x3d, 0x35, 0xa5, 0xb5, 0xb3, 0x7d, 0x5f, 0xb2, 0x3e, 0x9b, 0x0f, 0x18, 0x93, 0x4d, 0x29,
					0x41, 0x3d, 0xdc, 0xa0, 0x51, 0xa0, 0x66, 0x78, 0x83, 0xf9, 0x65, 0x50, 0xf6, 0xeb, 0x8b, 0x1c,
					0xdf, 0x47, 0xdb, 0x72, 0xed, 0xa3, 0x61, 0xd7, 0xfe, 0x6f, 0x6b, 0xf5, 0xab, 0xbc, 0x3f, 0x78,
					0x66, 0x18, 0x55, 0x65, 0x54, 0xe4, 0xb9, 0xe3, 0x19, 0xf0, 0xc2, 0x4a, 0x4d, 0x8f, 0x65, 0x82,
					0x83, 0x7f, 0x0f, 0xbf, 0xa7, 0x7f, 0x03, 0x1f, 0x7d, 0x86, 0xf8, 0xa1, 0xc3, 0xa1, 0xc8, 0x52,
					0xcf, 0x07, 0x41, 0x78, 0x4a, 0x05, 0xaa, 0x45, 0x5d, 0xf0, 0x89, 0x05, 0x2a, 0x1f, 0xf5, 0x0a,
					0x3f, 0xa2, 0x71, 0xf4, 0xc9, 0xb4, 0xa9, 0x54, 0xe3, 0xbd, 0xf9, 0x52, 0xff, 0x42, 0x32, 0x4d,
					0xd2, 0xc8, 0xda, 0xe8, 0xb9, 0x1b, 0xd4, 0x14, 0x85, 0xbc, 0xea, 0xae, 0x6d, 0x24, 0xd8, 0xe3,
					0x3e, 0x60, 0xb6, 0x77, 0xd2, 0xe0, 0xe0, 0x58, 0xb3, 0xdc, 0x69, 0xd4, 0xfa, 0xa0, 0x5a, 0xdf,
					0x3e, 0x17, 0x42, 0x73, 0xba, 0x7c, 0x1f, 0xa9, 0x6f, 0xdb, 0xe7, 0x77, 0xfa, 0x9f, 0x93, 0xf2,
					0xa8, 0xfb, 0xf4, 0x56, 0x89, 0x38, 0x00, 0xb6, 0xc9, 0x42, 0x2d, 0x86, 0x50, 0x0d, 0xe9, 0xc0,
					0xb6, 0x9b, 0x96, 0xf8, 0x23, 0x94, 0xcf, 0xbe, 0x7f, 0xc5, 0x64, 0x07, 0x43, 0x53, 0xcd, 0x10,
					0x11, 0x32, 0xf7, 0x9d, 0x27, 0xfb, 0x51, 0x98, 0xb2, 0x11, 0x98, 0x52, 0xb9, 0x7d, 0xc9, 0xdb,
					0x4b, 0xd2, 0xd3, 0x55, 0xee, 0xca, 0x76, 0x52, 0xc7, 0x75, 0x1f, 0xcf, 0xf8, 0x7e, 0xcc, 0x93,
					0xb6, 0x09, 0xfa, 0x3c, 0x5b, 0xe4, 0x22, 0x54, 0xf0, 0x72, 0x4d, 0x5e, 0x2c, 0x18, 0x1f, 0xd6,
					0xf0, 0xb9, 0xf8, 0x6b, 0x70, 0x86, 0x34, 0x3e, 0x53, 0x02, 0x7b, 0xf2, 0x99, 0xa1, 0xe1, 0xa7,
					0x57, 0xf9, 0xd4, 0x9f, 0xb6, 0xf0, 0x09, 0x48, 0xfa, 0x16, 0x62, 0xf3, 0x4c, 0xc9, 0x6f, 0xd0,
					0xfc, 0xc4, 0xc9, 0xe6, 0x23, 0x3e, 0x61, 0xe6, 0x20, 0x61, 0xe3, 0x20, 0xae, 0xdf, 0x66, 0x54,
					0x1c, 0x09, 0xb1, 0xba, 0xda, 0xc0, 0x73, 0x9b, 0x4c, 0xbb, 0x65, 0x65, 0x21, 0x2a, 0xd2, 0xd6,
					0xde, 0x42, 0xf5, 0xa9, 0xc0, 0x4a, 0x81, 0x98, 0xfc, 0x4d, 0x1e, 0xca, 0x7c, 0x62, 0x2f, 0xf6,
					0x89, 0xad, 0xa7, 0x0d, 0xa7, 0x48, 0xf4, 0x24, 0x4f, 0x9a, 0xbc, 0x34, 0xf1, 0x0c, 0xea, 0x55,
					0xeb, 0xf7, 0xd3, 0xd9, 0x71, 0x22, 0xdb, 0xb6, 0x43, 0x18, 0xc1, 0xe6, 0x6c, 0xd6, 0xd8, 0x84,
					0x44, 0xf8, 0x77, 0xc9, 0xdf, 0x80, 0xeb, 0xbb, 0x0a, 0x20, 0xfe, 0x7b, 0x26, 0x76, 0xa5, 0x81,
					0xd0, 0x36, 0xd6, 0xb9, 0x4e, 0xd6, 0x31, 0x2d, 0xd5, 0x97, 0x29, 0x45, 0x04, 0x88, 0xaa, 0x55,
					0xb3, 0x18, 0x7e, 0x83, 0x7f, 0x67, 0xd6, 0x49, 0xd5, 0x8a, 0x02, 0x14, 0xf2, 0xc7, 0x85, 0x82,
					0xda, 0x33, 0xa2, 0x43, 0x92, 0x6c, 0xc0, 0x04, 0x46, 0x92, 0xb7, 0xb7, 0x32, 0x01, 0x82, 0x86,
					0x1d, 0x47, 0x88, 0xe5, 0x74, 0xec, 0xbe, 0x9d, 0x35, 0xce, 0xf3, 0xe5, 0xb4, 0x03, 0xc3, 0x1a,
					0xad, 0x50, 0xc4, 0x91, 0xe3, 0x32, 0x5c, 0x03, 0xa9, 0x66, 0x01, 0x9b, 0x55, 0x29, 0xa1, 0x6c,
					0x8a, 0xd6, 0xd3, 0x0a, 0x66, 0x3b, 0xfb, 0x21, 0x3c, 0x78, 0x79, 0x86, 0xcf, 0xd0, 0x6d, 0x0c,
					0xa8, 0x5e, 0xf7, 0x78, 0x57, 0xbe, 0x44, 0x1f, 0x32, 0x59, 0x0a, 0x52, 0xb5, 0x9b, 0x96, 0xbb,
					0x7e, 0xcc, 0x8c, 0x25, 0xd1, 0x5b, 0x40, 0x5f, 0x2e, 0x23, 0x5f, 0x7e, 0x08, 0x0b, 0x06, 0x91,
					0xe5, 0xcc, 0xfe, 0xac, 0x81, 0xe6, 0xb7, 0x8f, 0x40, 0x1a, 0x4b, 0x95, 0x26, 0xd5, 0x58, 0x61,
					0x36, 0x54, 0xa6, 0xd6, 0xd4, 0x98, 0xba, 0xf6, 0x51, 0xbc, 0x98, 0x6e, 0x4a, 0x91, 0x26, 0xa8,
					0xcf, 0xeb, 0x66, 0x24, 0x47, 0x38, 0xad, 0x00, 0x25, 0xe1, 0x94, 0x43, 0x86, 0x84, 0x48, 0x6d,
					0x53, 0x5b, 0xa9, 0x62, 0x23, 0x33, 0xc7, 0xae, 0x3f, 0x3f, 0xd8, 0xd1, 0xf6, 0x61, 0x98, 0x55,
					0xbe, 0x45, 0x8d, 0x2a, 0x6f, 0xd1, 0x29, 0xa5, 0x21, 0xd1, 0xf7, 0x45, 0x81, 0x1f, 0xd6, 0x28,
					0x4f, 0xe8, 0x3e, 0x60, 0x4c, 0x5b, 0xe8, 0x8a, 0x02, 0x6c, 0xb2, 0xda, 0x6a, 0xcb, 0x98, 0x52,
					0xf1, 0xd7, 0x53, 0xc6, 0x8d, 0x77, 0xb9, 0x40, 0x21, 0x4e, 0x33, 0xd0, 0xb4, 0x31, 0xe4, 0xed,
					0x85, 0x40, 0xb6, 0xc8, 0x6c, 0xf6, 0x5e, 0x82, 0x55, 0x36, 0x99, 0xda, 0xa0, 0xd8, 0xa9, 0x1f,
					0xc9, 0xdb, 0x80, 0x56, 0xa5, 0xfc, 0x5f, 0xba, 0xab, 0xa2, 0xb9, 0xee, 0x75, 0x39, 0xa4, 0xbf,
					0xb1, 0x37, 0x91, 0x5a, 0x14, 0xc0, 0xa7, 0xc5, 0x8e, 0x78, 0xd8, 0x86, 0x28, 0x32, 0x3c, 0x0e,
					0x07, 0x0e, 0xc8, 0xaa, 0x4a, 0x51, 0xa7, 0xe5, 0x19, 0xab, 0xdc, 0xa0, 0xb7, 0x33, 0xdd, 0x90,
					0xca, 0x5c, 0xde, 0x3a, 0xc8, 0xea, 0x03, 0x0d, 0x20, 0xac, 0xb9, 0xe5, 0xb8, 0xb9, 0xf7, 0xdd,
					0x13, 0x90, 0x30, 0x5b
	};

	mbedtls_lmots_public_t pub_ctx;
	int ret;

	mbedtls_lmots_public_init(&pub_ctx);
	mbedtls_lmots_import_public_key(&pub_ctx, test_pubkey, sizeof(test_pubkey));

	ret = mbedtls_lmots_verify(&pub_ctx, msg->x, msg->len, test_dgst, sizeof(test_dgst));
	if(ret != 0) {
		printf("%s :: %d : failed, ret = %x\n", __FUNCTION__, __LINE__, -ret);
		goto exit;
	}
	printf("%s :: verify ok\n", __FUNCTION__);
exit:
	mbedtls_lmots_public_free(&pub_ctx);

	return 0;
}

/*
 * LMS test case: to verify digest with public key and message data, 
 * the below public key and digest data are generated by Cisco hash-sign test program with H=10
 */
int lms_sign_verify_test3(const struct shell *shell, data_t *msg)
{
	uint8_t test_pubkey[] ={
		0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0xfe, 0xce, 0xee, 0x22, 0xf3, 0x78, 0xa7, 0xd4,
		0xd9, 0xc5, 0x68, 0xcc, 0xdc, 0x29, 0x70, 0xbd, 0xf1, 0xde, 0x73, 0xa4, 0xef, 0x2f, 0x81, 0x2d,
		0x65, 0x8d, 0x0d, 0xf2, 0x2b, 0x73, 0x96, 0x24, 0x83, 0x46, 0x55, 0x5f, 0x99, 0x00, 0xd7, 0x58,
		0x15, 0x51, 0x61, 0x65, 0x1e, 0xe9, 0x6b, 0xb4
	};
	uint8_t test_dgst[] = { 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
		0x31, 0xfc, 0x3f, 0xa4, 0x3f, 0x40, 0x71, 0x9e, 0x6c, 0xef, 0x93, 0x85, 0x05, 0x61, 0xe4, 0x6c,
		0xfb, 0x72, 0xee, 0x42, 0xf2, 0x8f, 0x89, 0xa3, 0x89, 0x40, 0xa1, 0xf3, 0x87, 0x37, 0xd3, 0x99,
		0x32, 0x3e, 0x3b, 0xf6, 0x5a, 0xec, 0xae, 0x4e, 0x38, 0xdb, 0xd8, 0x4f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 0x33, 0x4f,
		0xf9, 0xd6, 0xd5, 0x76, 0xae, 0x8e, 0x80, 0xcb, 0xd2, 0x99, 0x33, 0x77, 0x1e, 0x4a, 0x30, 0xb0,
		0xdc, 0xb4, 0xee, 0x20, 0x58, 0x3b, 0x57, 0xce, 0xa3, 0xf9, 0xa8, 0x68, 0xc8, 0x0b, 0x00, 0x47,
		0x46, 0x85, 0x75, 0x4a, 0x70, 0x06, 0x71, 0xed, 0x18, 0x94, 0x99, 0x75, 0x0e, 0xdc, 0x2d, 0xcb,
		0xf5, 0x43, 0xde, 0x10, 0xe1, 0xad, 0x2a, 0xb0, 0x9d, 0x2e, 0x35, 0xba, 0x23, 0x98, 0x46, 0x8a,
		0x6c, 0x1f, 0x3d, 0x8c, 0x5a, 0xcc, 0x47, 0xc4, 0xb6, 0x7f, 0x46, 0x11, 0x60, 0x65, 0x2a, 0xb7,
		0xae, 0x58, 0xdc, 0x27, 0x53, 0x88, 0xca, 0xd7, 0xe4, 0xe9, 0x97, 0x24, 0x6b, 0x5f, 0x5a, 0x2e,
		0x4f, 0xb2, 0x37, 0x34, 0x24, 0x51, 0xd6, 0x59, 0x55, 0x0c, 0xcd, 0x88, 0x90, 0xc0, 0x69, 0xc0,
		0x4d, 0xe3, 0xad, 0x17, 0xbf, 0x62, 0xaa, 0x49, 0x62, 0x9b, 0x44, 0x3a, 0xae, 0x0d, 0x5a, 0xcf,
		0x92, 0x8f, 0x46, 0x43, 0x49, 0xc8, 0xad, 0xd0, 0x4c, 0x10, 0xd2, 0xb9, 0x53, 0x5b, 0x0d, 0x68,
		0x73, 0x7e, 0x08, 0x7d, 0xd7, 0x10, 0xa9, 0x63, 0x20, 0xdc, 0x17, 0xb0, 0x68, 0x98, 0x69, 0x27,
		0x03, 0x6b, 0x96, 0x16, 0xd6, 0x71, 0x2f, 0xb6, 0x3c, 0xe3, 0x99, 0x97, 0x93, 0x17, 0xdc, 0x35,
		0x3c, 0x68, 0xaa, 0xbe, 0x5a, 0xa4, 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x3c, 0x90, 0x5f, 0xaa, 0x68, 0xde, 0xbd, 0x7b,
		0x95, 0x27, 0xc4, 0xd0, 0x52, 0x2f, 0x14, 0x94, 0xf5, 0x37, 0x34, 0x08, 0x11, 0x4a, 0xb2, 0x4f,
		0xcc, 0x8e, 0xe0, 0x88, 0x56, 0x28, 0xc5, 0x1a, 0xdf, 0x94, 0x43, 0xc8, 0x3c, 0xed, 0xfd, 0x2a,
		0xa0, 0x75, 0x59, 0x3b, 0x16, 0xc4, 0x6b, 0x6e, 0x5b, 0x8c, 0xa3, 0x67, 0x99, 0xa5, 0x87, 0x0a,
		0x31, 0x83, 0x14, 0xa2, 0x12, 0x3d, 0x7b, 0x1f, 0x0f, 0xe6, 0x04, 0xfe, 0xfc, 0x0a, 0xca, 0x60,
		0xbd, 0xe1, 0x85, 0xdf, 0xdc, 0x31, 0x63, 0xd0, 0xcc, 0x3d, 0x36, 0xbb, 0xa7, 0x22, 0x7e, 0x6d,
		0xf4, 0x31, 0xd2, 0xd1, 0x1c, 0x28, 0x64, 0x1d, 0xd4, 0xa6, 0x8f, 0x8a, 0x97, 0xda, 0x64, 0x4f,
		0x76, 0x5a, 0xac, 0xe4, 0x4c, 0x37, 0x73, 0x7e, 0x2f, 0x63, 0x47, 0xf6, 0x68, 0x4f, 0x7f, 0x96,
		0x61, 0x6d, 0xa9, 0x2d
	};

	mbedtls_lms_public_t pub_ctx;
	int ret;

	mbedtls_lms_public_init(&pub_ctx);
	ret = mbedtls_lms_import_public_key(&pub_ctx, test_pubkey, sizeof(test_pubkey));
	if (ret != 0)
	{
		printf("import public key failed, ret = %x\n", ret);
		return -1;
	}

	ret = mbedtls_lms_verify(&pub_ctx, msg->x, msg->len, test_dgst, sizeof(test_dgst));
	if (ret != 0)
	{
		printf("verify failed, ret = %x, sig size = %d\n", -ret, sizeof(test_dgst));
		return -1;
	}
	printf("%s :: verify ok\n", __FUNCTION__);
	return 0;
}

/*
 * LMS test case: to verify digest with public key and message data, 
 * the below public key and digest data are generated by mbedTLS with H= 5
 */
int lms_sign_verify_test4(const struct shell *shell, data_t *msg)
{
	uint8_t test_pubkey[] ={
		0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x04, 0xa2, 0x33, 0xfb, 0x85, 0x43, 0x08, 0xdc, 0x8b,
		0x96, 0x9a, 0x2e, 0x50, 0x68, 0x3a, 0xe2, 0x75, 0x22, 0xcc, 0xc4, 0xd2, 0x54, 0x4f, 0xcb, 0x31,
		0xd4, 0xb5, 0x61, 0x6b, 0x64, 0x23, 0x5f, 0x19, 0xf3, 0x9c, 0xd7, 0xcb, 0x00, 0xf2, 0x05, 0xcc
	};
	uint8_t test_dgst[] = { 
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0xc9, 0x23, 0x60, 0xa7, 0xd8, 0xef, 0x3c, 0xb5,
		0xfc, 0xf1, 0x09, 0xba, 0x98, 0x0b, 0x95, 0x20, 0x58, 0x04, 0x64, 0x23, 0x59, 0x55, 0x88, 0x60,
		0xc0, 0x3a, 0xbc, 0x9b, 0xa8, 0x0d, 0x1e, 0xbb, 0xdd, 0xe1, 0x52, 0x27, 0xd8, 0x5b, 0x70, 0x9d,
		0x69, 0x23, 0xc9, 0x10, 0x26, 0xeb, 0xaf, 0xa0, 0xb5, 0x72, 0x62, 0x76, 0xf3, 0xfb, 0xc3, 0x6d,
		0x6c, 0x23, 0x80, 0x81, 0x2b, 0x45, 0x50, 0x97, 0x8e, 0x59, 0x96, 0x1b, 0xcd, 0x55, 0x24, 0x10,
		0xa5, 0x02, 0xd0, 0xe6, 0x4c, 0xaa, 0x6d, 0x6b, 0xc7, 0x3c, 0xd5, 0xe4, 0x58, 0x40, 0xf4, 0x7f,
		0x4a, 0xf6, 0x5a, 0xc0, 0x05, 0x93, 0x7a, 0x61, 0xd1, 0x20, 0xbc, 0x3b, 0xbb, 0x67, 0xb6, 0xeb,
		0xab, 0x8f, 0xa0, 0xdc, 0x73, 0x16, 0x07, 0x97,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x83, 0x93, 0x4c, 0x45, 0x15, 0xf5, 0x5f, 0x88,
		0xc3, 0x9e, 0xfe, 0x99, 0x89, 0xae, 0x3a, 0xb3, 0x74, 0x87, 0x3c, 0x2b, 0xdd, 0x4b, 0x55, 0x83,
		0x85, 0xb9, 0xee, 0x00, 0xe0, 0x90, 0x58, 0x1e, 0xc3, 0x77, 0xcb, 0x1a, 0x4f, 0xd2, 0xde, 0x16,
		0xf6, 0x1e, 0xb1, 0x44, 0xb7, 0x82, 0x05, 0xb4, 0xa4, 0xe1, 0x90, 0xd0, 0x73, 0x5c, 0xc8, 0x43,
		0x63, 0xfa, 0xbf, 0x2e, 0xba, 0xc0, 0x80, 0x18, 0x4a, 0xae, 0x42, 0xce, 0x3b, 0x98, 0x92, 0xc2,
		0x04, 0x6f, 0x11, 0xe6, 0x11, 0xb2, 0xcc, 0x40, 0xae, 0x07, 0xd8, 0xf4, 0xfc, 0x36, 0x68, 0x83,
		0xdb, 0x7b, 0xa5, 0x5a, 0x8e, 0x10, 0xaf, 0xbd, 0x00, 0x00, 0x00, 0x0a, 0x29, 0x46, 0x90, 0x44,
		0xc8, 0x42, 0x32, 0xcc, 0x09, 0xb0, 0x61, 0x7d, 0x5b, 0x93, 0x1c, 0x2e, 0x1a, 0xb0, 0x6d, 0xed,
		0x85, 0x91, 0x8b, 0x3a, 0x20, 0x9b, 0xea, 0xd4, 0xe3, 0x38, 0x05, 0x85, 0x5e, 0x6d, 0x50, 0x54,
		0x60, 0x64, 0x30, 0x72, 0xaf, 0xae, 0x26, 0xea, 0x21, 0x34, 0xef, 0x70, 0x9a, 0x56, 0xcb, 0x40,
		0x11, 0x09, 0x28, 0x8b, 0xe4, 0x11, 0x6b, 0x45, 0x58, 0x7f, 0x97, 0xcb, 0xee, 0x65, 0xfb, 0x80,
		0xce, 0xe9, 0x5d, 0x6a, 0x43, 0x8d, 0xec, 0xc9, 0x15, 0x62, 0x8a, 0x55, 0x0d, 0x4a, 0xd7, 0x1e,
		0xe5, 0x95, 0xe6, 0xb1, 0x57, 0xa3, 0xbc, 0x9b, 0xbb, 0x46, 0x0d, 0x3f, 0x48, 0x18, 0x25, 0x89,
		0x48, 0x5e, 0x6d, 0xb4, 0x05, 0xbe, 0xf9, 0x25, 0x9e, 0xd6, 0x46, 0xd7, 0x88, 0x46, 0x9b, 0x6a,
		0x8f, 0xbe, 0x20, 0x4a
	};

	mbedtls_lms_public_t pub_ctx;
	int ret;

	mbedtls_lms_public_init(&pub_ctx);
	ret = mbedtls_lms_import_public_key(&pub_ctx, test_pubkey, sizeof(test_pubkey));
	if (ret != 0)
	{
		printf("import public key failed, ret = %x\n", ret);
		return -1;
	}

	ret = mbedtls_lms_verify(&pub_ctx, msg->x, msg->len, test_dgst, sizeof(test_dgst));
	if (ret != 0)
	{
		printf("verify failed, ret = %x, sig size = %d\n", -ret, sizeof(test_dgst));
		return -1;
	}
	printf("%s :: verify ok\n", __FUNCTION__);
	return 0;
}

static int mbedtls_lms_test(const struct shell *shell, int argc, char *argv[])
{
	uint32_t time_start;
	uint32_t time_end;

	data_t lmots_msg = {"c41ba177a0ca1ec31dfb2e145237e65b", 32};

#if defined(MBEDTLS_LMS_PRIVATE)
	data_t lmots_key_id = {"00000000000000000000000000000000", 32},
		lmots_seed = {"403cbcc9808bb4b5ad72476ea297b2854c928ff5336f0b98ac2237ec83225ae7", 64},
		lms_msg = {"c41ba177a0ca1ec31dfb2e145237e65b", 32},
		lms_seed = {"626201f41afd7c9af793cf158da58e33", 32};
#endif

#if defined(MBEDTLS_LMS_PRIVATE)
	time_start = k_uptime_get_32();
	lmots_sign_verify_test1(shell, &lmots_msg, &lmots_key_id, 0, &lmots_seed);
	time_end = k_uptime_get_32();
	printf("Case 1 : LM-OTS full test : %d\n", time_end-time_start);

	time_start = k_uptime_get_32();
	lms_sign_verify_test(shell, &lms_msg, &lms_seed);
	time_end = k_uptime_get_32();
	printf("Case 2 : LMS full test : %d\n", time_end-time_start);

#endif

	time_start = k_uptime_get_32();
	lmots_sign_verify_test2(shell, &lmots_msg);
	time_end = k_uptime_get_32();
	printf("Case 3 : LM-OTS verification test : %d\n", time_end-time_start);

	time_start = k_uptime_get_32();
	lms_sign_verify_test3(shell, &lmots_msg);
	time_end = k_uptime_get_32();
	printf("Case 4 : LMS (H=10) verification test : %d\n", time_end-time_start);

	time_start = k_uptime_get_32();
	lms_sign_verify_test4(shell, &lmots_msg);
	time_end = k_uptime_get_32();
	printf("Case 5 : LMS (H=5) verification test : %d\n", time_end-time_start);

	return 0;
}

SHELL_STATIC_SUBCMD_SET_CREATE(lms_cmds,
							   SHELL_CMD_ARG(test, NULL, "", mbedtls_lms_test, 1, 0),
							   SHELL_SUBCMD_SET_END);

SHELL_CMD_REGISTER(lms, &lms_cmds, "LMS shell commands", NULL);
#endif // #if defined(CONFIG_LMS_SHELL)
